<?php
namespace app\index\controller\tan;
/**
 * 剔除多余时key and value
 */
class Eliminate{
    /**
     * 插入更新剔除多余key
     * @param array $data  原数据
     * @param array $rule   规则
     * @return data
     */
    static public function E(array $data,array $rule ){
         $rule= array_keys($rule);
         $rule=preg_replace("/\|{1}[a-zA-z0-9\x{4e00}-\x{9fa5}]+/u",  "",$rule );
         $rule=array_fill_keys ($rule,"");
        return  array_intersect_key($data, $rule);
    }
    /**
     * 查询 剔除多余和不满足条件的参数，<b>支持自定义判断</b>
     * <i></i>
     * <p>注意：<b>该方法默认被处理的参数都是   and   where(['s.asaa'=>32,'b'=>'asd'])   </b>
     * @param array $key        要保留的key  支持数据格式：<b> ['表名.字段名',"字段名称"]</b>
     * @param array $Delivery   post或get传输过来的数据
     * @param array $keyFun     自定义判断的匿名函数，返回bool
     * @return array
     */
    static public function ESearch(array $key,array $Delivery,array $keyFun=[]){
        $key1=preg_replace("/.+\./u",  "",$key );
        $key2=array_combine($key1,$key);
        $data= array_intersect_key($Delivery, $key2);
        $Search=[];
        foreach ($key2 as $Key=>$value){
            if(! array_key_exists($Key, $data)){
                continue;
            }
            if(array_key_exists($value,  $keyFun)){
                self::Fun($keyFun[$value], $data[$Key], $Search,$value);
            } else {
                #默认    如果：$data[$Key] 为true   则存入search数组;
               $data[$Key] && $Search[$value]=$data[$Key];
            }
        }
        #返回满足要求的搜索条件
        return $Search;
    }
    /**
     * 将满足条件的存入数组
     * @param \Closure $Fun
     * @param type $value
     * @param type $data
     * @param type $key
     */
    private static function Fun(\Closure $Fun,$value,&$data,$key){
        if($Fun($value)){
            $data[$key]=$value;
        }
    }
    public static function EWhere(array $data,$fs=[]){
//        where xx=3 or 34='43'
        $where=[];
        foreach($data as $key=>$value){
            if(array_key_exists($key, $fs)){
                $key2=key($fs[$key]);
                if(is_array($fs[$key][$key2])){
                    self::X($where,$fs[$key] ,$key,$value );
                }else{
                    $where[$key]=[$key2 , sprintf( $fs[$key][$key2]  ,$value  )  ];
                }
            }else{
                $where[$key]=["=",$value    ];
            }
        }
        return $where;
    }
    private static function X(&$where,$array,$key,$value){
        $k2=key($array);
        $where[$key]=[key($array), sprintf($array[$k2][0] ,$value)  ];
        unset($array[$k2][0]);
        foreach($array[$k2]  as $Key=> $arr){
            $where[$Key]=[ $arr[0] , sprintf($arr[1]  ,$value)   ];
        }
    }
}
#SELECT * FROM `users` `u` WHERE `u`.`name` LIKE '谭%' AND `u`.`is_business` < '32' AND `u`.`is_address` <> '32'
//        $data=[
//             "u.name"=>"谭",
//            "u.is_business"=>32,
//         ];
//        $d= tan\Eliminate::EWhere($data,[
//            "u.name"=>["like"=>"%s%%"],
//            "u.is_business"=>[
//                "<"=>[
//                    "%s",
//                        "u.is_address"=>[
//                          "<>","%s"
//                        ],
//                    ]
//            ]
//        ]); 
//        echo "<pre>";
//        print_r($d);#die;
//         echo "</pre>";
//        Db::table("users")->alias("u") ->where($d)->select();
//
//        return;